package com.allen.service.impl;

import com.allen.bean.Dish;
import com.allen.bean.Setmeal;
import com.allen.bean.SetmealDish;
import com.allen.common.CustomException;
import com.allen.dao.SetmealDao;
import com.allen.dto.SetmealDto;
import com.allen.service.SetmealDishService;
import com.allen.service.SetmealService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author dragon1006xu@gmail.com
 * @create 2022/2/17 19:25
 */
@Service
@Slf4j
public class SetmealServiceImpl extends ServiceImpl<SetmealDao, Setmeal> implements SetmealService {
    @Autowired
    SetmealDishService setmealDishService;

    @Override
    @Transactional
    public void saveWithDish(SetmealDto setmealDto) {
        //保存套餐信息到Setmeal
        this.save(setmealDto);
        //保存套餐下的菜品信息到setmeal_dish中
        List<SetmealDish> setmealDishList = setmealDto.getSetmealDishes();
        //循环遍历 该菜品插入到setmeal_dish表中 要知道该菜品属于哪个套餐 所以循环遍历设置每一个菜品对应的套餐id
        Long setmealId = setmealDto.getId();//套餐id
        for (SetmealDish setmealDish : setmealDishList) {
            setmealDish.setSetmealId(setmealId);
        }
        //批量保存套餐菜品信息到setmeal_dish表中
        setmealDishService.saveBatch(setmealDishList);
    }

    @Override
    @Transactional
    public void removeWithDish(List<Long> ids) {
        //select count(*) from setmeal where id in(1,2,3) and status=1
        //查询套餐状态，确定是否删除
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(Setmeal::getId, ids);
        lambdaQueryWrapper.eq(Setmeal::getStatus, 1);
        int count = this.count(lambdaQueryWrapper);
        if (count > 0) {
            //如果不能删除，抛出一个业务异常
            throw new CustomException("套餐正在售卖中，不能删除");
        }
        //如果可以删除，先删除套餐表中的数据
        this.removeByIds(ids);
        // delete from setmeal_dish where setmeal_id in(1,2,3)
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(SetmealDish::getSetmealId, ids);
        //删除关系表中的数据---setmeal_dish
        setmealDishService.remove(wrapper);

    }

    /**
     * 更新套餐状态
     *
     * @param ids
     * @param status
     */
    @Override
    public void updateStatus(List<Long> ids, Integer status) {
        //根据status修改停售或启售状态
        //select * from setmeal where id in(1,2,3) and status=1;
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId, ids);
        List<Setmeal> setmealList = this.list(queryWrapper);
        for (Setmeal setmeal : setmealList) {
            if (setmeal.getStatus() != status) {
                setmeal.setStatus(status);
                this.updateById(setmeal);
            }
        }
    }

    /**
     * 根据id查询套餐信息和套餐对应的菜品信息
     * @param id
     * @return
     */
    @Override
    public SetmealDto getByIdWithDish(Long id) {
       //根据套餐id查询套餐信息
        Setmeal setmeal = this.getById(id);
        //创建SetmealDto对象
        SetmealDto setmealDto = new SetmealDto();
        //将setmeal对象数据封装到setmealDto
        BeanUtils.copyProperties(setmeal,setmealDto);
        //查询当前套餐对应的菜品信息，从SetmealDish表查询
        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
        List<SetmealDish> setmealDishList = setmealDishService.list(lambdaQueryWrapper);
        setmealDto.setSetmealDishes(setmealDishList);
        return setmealDto;
    }

    /**
     * 修改套餐信息
     * @param setmealDto
     */
    @Override
    @Transactional
    public void updateWithDish(SetmealDto setmealDto) {
        //修改套餐基本信息
        this.updateById(setmealDto);
        //修改套餐对应的菜品信息
        //思路：修改中有新增菜品，有修改菜品信息，删除菜品操作。无法通过原有的数据进行修改
        //解决方法：删除套餐下菜品信息，重新新增套餐对应菜品信息
        Long setmealId = setmealDto.getId();//套餐id
        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SetmealDish::getSetmealId,setmealId);
        //删除套餐下原有的菜品
        setmealDishService.remove(lambdaQueryWrapper);
        //新增套餐下当前的菜品
        List<SetmealDish> setmealDishList = setmealDto.getSetmealDishes();
        log.info("-----------套餐下菜品列表--------：{}",setmealDishList);
        for (SetmealDish setmealDish : setmealDishList) {
            setmealDish.setSetmealId(setmealId);

            }
        setmealDishService.saveBatch(setmealDishList);

        }


    }

